/**
 * express.js Cookie示例
 */

const express = require("express")

//导入cookie模块
const cookieParser = require("cookie-parser")
// 创建express应用
const app = express()
// express http服务端口
const port = 3001

app.use(express.json());
app.use(express.urlencoded({extended:false}));

app.use(cookieParser());

app.get("/",function(req,res){
    if(req.cookies.remember){
        res.send(`记住了。<a href="/forget>忘记</a>!`);
    }else{
        let str = `
            <form method="post">
                <label>
                    <input type="checkbox" name="remember"/>记住
                </label>
                <input type="submit" value="提交"/>
            </form>`;

        res.send(str);
    }
})

app.get("/forget",function(req,res){
    res.clearCookie("remeember");
    res.redirect("back");
})

app.post("/",function(req,res){
    let minute = 6000;
    if(req.body.remember)
        res.cookie("remember",1,{maxAge:minute});
    
    res.redirect("back");
})

// 启动http服务
app.listen(port,function(){
    console.log(`myapp访问地址：http://127.0.0.1:${port}`)
})